'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/11/5 20:53
 * @ description：
 * @ modified By：
 * @ ersion     : 
 * @File        : test.py 
'''

import numpy as np
import matplotlib.pyplot as plt

def _conv_epoch(src_block,filter):
    input = src_block.flatten()
    filter = filter.flatten().T

    return np.dot(input,filter)
"""
input_size:(h,w,c)
filter_size:(h,w,ic,oc)
"""
#conv(img,img.shape,(3,3,img.shape[2],3))
def conv(img,input_size,filter_size,stride=1):

    ih = input_size[0]
    iw = input_size[1]
    ic = input_size[2]

    filter_oc = filter_size[3]
    print("filter_oc:",filter_oc)
    filter_h = filter_size[0]
    filter_w = filter_size[1]
    filter_ic = filter_size[2]

    l = int((ih - filter_h) / stride + 1)
    m = int((iw - filter_w) / stride + 1)

    result = np.zeros(shape=(l,m,filter_oc),dtype=np.uint8)

    for i in range(l):
        for j in range(m):
            for k in range(filter_oc):
                f = np.random.uniform(0,1,filter_w*filter_h*filter_ic).T
                input = img[i:i+filter_h,j:j+filter_w,:]
                # print(input.shape)

                result[i,j,k] = _conv_epoch(input,f)

    return result
def test():
    img = plt.imread("D:/python/data/cat/cat.4003.jpg")
    # n=20
    # img = np.random.randn(10, n, n, 3)

    print("img shape ",img.shape)

    result = conv(img,img.shape,(3,3,img.shape[2],3))

    plt.figure()
    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(result)
    plt.show()

test()
